<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\cms\home;

use app\cms\model\Column as ColumnModel;
use app\cms\model\Document as DocumentModel;
use util\Tree;
use think\Db;

/**
 * 文档控制器
 * @package app\cms\home
 */
class Document extends Common
{

    public function lightrainnews($id=0){
        $data=Db::name('wenzhang')
            ->where(['id'=>$id])
            ->find();
        $this->assign('document',$data);
        return $this->fetch();
    }
    /**
     * 文档详情页
     * @param null $id 文档id
     * @param string $model 独立模型id
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     */
    public function detail($id = null, $model = '')
    {
        //$jssdk = new Jssdk('wx778f04d81a2159a0','e9f757c60460a067716c59cb1a55000b');
        //$signPackage = $jssdk->GetSignPackage();
        //dump($signPackage);die;
        //$this->assign('sign', $signPackage);
        if ($id === null) $this->error('缺少参数');

        $ip = ip();
        $findip = Db::table('dp_cms_viewip')
            ->where(['aid'=>$id,'ip'=>$ip])
            ->find();
//        halt($ip);
        if(!$findip){
            //增加计数器
            Db::table('dp_cms_document')
                ->where('id','=',$id)
                ->setInc('view');
            Db::table('dp_cms_viewip')
                ->insert(['aid'=>$id,'ip'=>$ip]);
            //删除ip数据库
            $maxnum=Db::table('dp_cms_viewip')
                    ->order('id desc')
                    ->find()['id']-10000;
            Db::table('dp_cms_viewip')
                ->where('id','<',$maxnum)
                ->delete();

        }


        if ($model != '') {
            $table = get_model_table($model);
            $map = [
                $table.'.status' => 1,
                $table.'.trash'  => 0
            ];
        } else {
            $map = [
                'cms_document.status' => 1,
                'cms_document.trash'  => 0
            ];
        }

        $info = DocumentModel::getOne($id, $model, $map);
        if (isset($info['tags'])) {
            $info['tags'] = explode(',', $info['tags']);
        }else{
        }

        /*随机列出搞笑图片链接
        $biaoqingbao=scandir('./biaoqingbao');
        foreach ($biaoqingbao as $key=>$v){
            //echo $v;
            if ($v=='.' || $v=='..'){
                unset($biaoqingbao[$key]);
            }
        }
        $randbiaoqingbao=($biaoqingbao[array_rand($biaoqingbao,1)]);
        $this->assign('randbiaoqingbao',$randbiaoqingbao);
        */
        //随便看看
        $rand=Db::name('cms_document')
            ->column('id');
        $arr=array_rand($rand,5);
        foreach ($arr as $v){
            $arr3[]=$rand[$v];
        }
        $rand2=Db::name('cms_document')
            ->where('id','in',$arr3)
            ->select();
        $this->assign('rand2', $rand2);

        //一月内访问最多文章
        $oneweek=Db::name('cms_document')
            ->order('view desc')
            ->where('create_time','>',time()-(60*60*24*30))
            ->limit(5)
            ->select();
        $this->assign('oneweek', $oneweek);

        //最新文章
        $new=Db::name('cms_document')
            ->order('id desc')
            ->limit(5)
            ->select();
        $this->assign('new', $new);

        $img='/static/img/sh.jpeg';
        $data=$info->toarray();
        $info=$info->toarray();
        $info['picture']=Db::name('admin_attachment')
            ->where('id','=',$info['picture'])
            ->value('path');
        $data['img']=$img;
        $this->assign('document', $info);
        $this->assign('data', $data);
        //dump($info->toarray());die;
        $this->assign('breadcrumb', $this->getBreadcrumb($info['cid']));
        $this->assign('prev', $this->getPrev($id, $model));
        $this->assign('next', $this->getNext($id, $model));

        $template = $info['detail_template'] == '' ? 'detail' : substr($info['detail_template'], 0, strpos($info['detail_template'], '.'));
        return $this->fetch($template);
    }

    /**
     * 获取栏目面包屑导航
     * @param int $id 栏目id
     * @author 蔡伟明 <314013107@qq.com>
     */
    private function getBreadcrumb($id)
    {
        $columns = ColumnModel::where('status', 1)->column('id,pid,name,url,target,type');
        foreach ($columns as &$column) {
            if ($column['type'] == 0) {
                $column['url'] = url('cms/column/index', ['id' => $column['id']]);
            }
        }
        return Tree::config(['title' => 'name'])->getParents($columns, $id);
    }

    /**
     * 获取上一篇文档
     * @param int $id 当前文档id
     * @param string $model 独立模型id
     * @author 蔡伟明 <314013107@qq.com>
     * @return array|false|\PDOStatement|string|\think\Model
     */
    private function getPrev($id, $model = '')
    {
        if ($model == '') {
            $cid = Db::name('cms_document')->where('id', $id)->value('cid');
            $document = Db::name('cms_document')->where([
                'status' => 1,
                'trash'  => 0,
                'cid'    => $cid,
                'id'     => ['lt', $id]
            ])->order('id desc')->find();
        } else {
            $table = get_model_table($model);
            $cid   = Db::table($table)->where('id', $id)->value('cid');
            $document = Db::table($table)->where([
                'status' => 1,
                'trash'  => 0,
                'cid'    => $cid,
                'id'     => ['lt', $id]
            ])->order('id desc')->find();
        }

        if ($document) {
            $document['url'] = url('cms/document/detail', ['id' => $document['id'], 'model' => $model]);
        }
        return $document;
    }

    /**
     * 获取下一篇文档
     * @param int $id 当前文档id
     * @param string $model 独立模型id
     * @author 蔡伟明 <314013107@qq.com>
     * @return array|false|\PDOStatement|string|\think\Model
     */
    private function getNext($id, $model = '')
    {
        if ($model == '') {
            $cid = Db::name('cms_document')->where('id', $id)->value('cid');
            $document = Db::name('cms_document')->where([
                'status' => 1,
                'trash'  => 0,
                'cid'    => $cid,
                'id'     => ['gt', $id]
            ])->find();
        } else {
            $table = get_model_table($model);
            $cid   = Db::table($table)->where('id', $id)->value('cid');
            $document = Db::table($table)->where([
                'status' => 1,
                'trash'  => 0,
                'cid'    => $cid,
                'id'     => ['gt', $id]
            ])->find();
        }

        if ($document) {
            $document['url'] = url('cms/document/detail', ['id' => $document['id'], 'model' => $model]);
        }

        return $document;
    }
}